package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import DataObjects.Bon;
import DataObjects.ConstantObjects;
import DataObjects.Menu;
import DataObjects.MetaDataOrder;
import DataObjects.ReportBon;
import Messages.MessageCheckExistsEmpForClockInOutReply;
import Messages.MessageCloseTable;
import Messages.MessageSaveTableOrder;
import Messages.MessageSavingReply;

import java.awt.Color;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class PfPtihatShulhanLeftRigtPanels extends JPanel{
	
	private Dimension screenSize;
	
	private MainMenu mainMenu;
	private Vector<Menu> menues;
	private Vector<Bon> bones;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel bonPanel;
	private JPanel buttonPanel;
	private JPanel recipesPanel;
	private JPanel menuButtonsPanel;
	private MetaDataOrder metaDataOrder;
	private PfPtihatShulhanLeftRigtPanels leftRightP;
	private int x;
	private int y;
	
	
	public PfPtihatShulhanLeftRigtPanels (MainMenu mm, Vector<Menu> m,Vector<Bon> vector, MetaDataOrder metaDataOrder)
	{
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		this.mainMenu=mm;
		this.menues=m;
		this.bones=vector;
		this.metaDataOrder=metaDataOrder;		
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		leftRightP=this;
		initialize();
	}
	
	private void initialize()
	{	
		add(getLeftPanel());  		
		add(getRightPanel());	
	}

	
	
	private JPanel getLeftPanel()            //search by category panel and choose specific item panel 
	{		
		leftPanel=new JPanel();
		leftPanel.setBackground(new Color(248, 248, 255));
		leftPanel.setBorder(new TitledBorder(null, "BON ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		BonPanel bonPanel=new BonPanel(bones,leftRightP);
		
		JScrollPane scroll=new JScrollPane(bonPanel);
		scroll.setPreferredSize(new Dimension(580, 550));
		leftPanel.add(scroll);
		
		return leftPanel;
	}
	
	
	
	private JPanel getRightPanel()           
	{
		
		rightPanel=new JPanel();
		rightPanel.setBorder(new TitledBorder(null, "MENU ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
		rightPanel.add(getMenuButtonsPanel());
		rightPanel.add(getMenuRecipesPanel());
		return rightPanel;
	}
	
	
	private JPanel getMenuRecipesPanel()
	{
		recipesPanel = new JPanel();
		recipesPanel.setBackground(new Color(250, 250, 210));
		recipesPanel.setBorder(new TitledBorder(null, "תפריטים", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		//GridLayout grid =new GridLayout(0, 5, 10, 20);
		//recipesPanel.setLayout(grid);
		recipesPanel.setPreferredSize(new Dimension(x/3, y/2));
		recipesPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5 ));
		ConstantObjects consobj=new ConstantObjects();
		String [] categories=consobj.getMenuCategories();
		for(int i=0; i<menues.size();i++)
		{		
			recipesPanel.add(getCategoryButton(menues.get(i)));
		}
		return recipesPanel;
	}
	
	
	
	
	
	
	private JButton getCategoryButton(Menu m)
	{
		final Menu menu=m;
		
		JButton catButt=new JButton(menu.getName());
		catButt.setPreferredSize(new Dimension(x/10,y/9));
		//catButt.setBackground(new Color(180,216,230));
		
		catButt.setFont(new Font("Tahoma", Font.BOLD, 15));
		catButt.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			
			{
				int isSelectableFlag=0;
				for(int i=0;i<menu.getRecipes().size();i++)
				{
					if(menu.getRecipes().get(i).getIsSelectable()==1)
					{
						isSelectableFlag=1;
					break;
					}
				}
				
				if(isSelectableFlag==0)
				{
					
					Bon bon=new Bon();
					bon.setMid(menu.getId());
					bon.setMname(menu.getName());
					bon.setPrice(menu.getPrice());
					bon.setRid("0");
					bon.setRname("אין");
					bon.setTid(metaDataOrder.getTid());
					bon.setType(menu.getType());
					bon.setEid(metaDataOrder.getEid());
					bon.setComments("אין");
					bones.add(bon);
					//leftPanel.repaint();
					//mainMenu.getContentPane().validate();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfPtihatShulhanLeftRigtPanels(mainMenu,menues,bones,metaDataOrder), BorderLayout.CENTER);					
					mainMenu.getContentPane().validate();
					
				}
				else
				{
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfPtihatShulhanRecipesToChoose(mainMenu,menues,menu,bones,metaDataOrder), BorderLayout.CENTER);					
					mainMenu.getContentPane().validate();
				}
			}
		});
		return catButt;
	}
	
	
	
	private JPanel getMenuButtonsPanel()
	{
		menuButtonsPanel = new JPanel();
		menuButtonsPanel.setPreferredSize(new Dimension(x/3, y/4));
		menuButtonsPanel.setBackground(new Color(238, 232, 170));
		menuButtonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5 ));
		
		JButton butHeshbon=new JButton("חשבון");
		butHeshbon.setForeground(new Color(0, 0, 139));
		butHeshbon.setPreferredSize(new Dimension(x/10,y/9));
		butHeshbon.setFont(new Font("Tahoma", Font.BOLD, 17));
		butHeshbon.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				ArrayList<ReportBon> bons = new ArrayList<ReportBon>();
				for(int i=0;i<bones.size();i++)
				{
					ReportBon bon=new ReportBon();
					//bon.setOrderNo("1");
					bon.setAmount("1");
					//bon.setTableNo(metaDataOrder.get);
					//bon.setMeltzarName("Alex");
					bon.setMid(bones.get(i).getMid());
					bon.setmName(bones.get(i).getMname());
					bon.mName = bones.get(i).getMname();
					bon.setPrice(bones.get(i).getPrice());
					bon.setRid(bones.get(i).getRid());
					bon.setrName(bones.get(i).getRname());
					bon.setTid(metaDataOrder.getTid());
					bon.setType(bones.get(i).getType());
					bon.setEid(metaDataOrder.getEid());
					bon.setComments(bones.get(i).getComments());
					bons.add(bon);
					
				}
				
				try {
					//Это параметры для номера заказа, столика и оффициант
					HashMap params = new HashMap();
					params.put("meltzarName", metaDataOrder.getName());
					params.put("tableNo",metaDataOrder.getTid());
					params.put("orderNo",metaDataOrder.getOrderId());
					
					//Это скармливание репорту списка бонов
					JRBeanCollectionDataSource reportData = new JRBeanCollectionDataSource(
							bons);
					JasperPrint print=new JasperPrint();
				//Это запуск показа репорта с параметрами и самими бонами
					print = JasperFillManager
							.fillReport(
									"Reports/Orders/CheckOut.jasper",
									params, reportData);
					JasperViewer.viewReport(print,false);
					
					
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				
				
				
			}
		});
		
		
		JButton butSave = new JButton("שמור");
		butSave.setForeground(new Color(0, 0, 139));
		butSave.setPreferredSize(new Dimension(x/10,y/9));
		//butSave.setBackground(new Color(211, 211, 211));		
		butSave.setFont(new Font("Tahoma", Font.BOLD, 17));
		butSave.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				
				MessageSaveTableOrder msgSave= new MessageSaveTableOrder(bones,metaDataOrder);
				try {
					mainMenu.ms.client.sendToServer(msgSave);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					if(result.getResult()==1)
					JOptionPane.showMessageDialog(null,"הנתונים נשמרו בהצלחה");
					else
					JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
				
				} catch (IOException ei) {
					// TODO Auto-generated catch block
					ei.printStackTrace();
				}
				
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfManageTableOrderPanel(mainMenu,metaDataOrder.getTid()), BorderLayout.CENTER);					
				mainMenu.getContentPane().validate();
				
			}
		});
		
		
		
		
		JButton butCancel=new JButton("ביטול");
		butCancel.setForeground(new Color(139, 0, 0));
		butCancel.setPreferredSize(new Dimension(x/10,y/9));
		//butCancel.setBackground(new Color(248, 248, 255));		
		butCancel.setFont(new Font("Tahoma", Font.BOLD, 17));
		butCancel.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfScheem_All_Tables(mainMenu),BorderLayout.CENTER);
				mainMenu.getContentPane().validate();				
			}
		});
		
		JButton butReturn = new JButton("סגירת שולחן");
		butReturn.setForeground(new Color(47, 79, 79));
		butReturn.setPreferredSize(new Dimension(x/10,y/9));
		//butReturn.setBackground(new Color(248, 248, 255));		
		butReturn.setFont(new Font("Tahoma", Font.BOLD, 16));
		butReturn.addActionListener(new java.awt.event.ActionListener() 
		   {
			
			
			
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");				
				Date date = new Date();
				String orderDate=dateFormat.format(date);
				String orderTime=timeFormat.format(date);
				metaDataOrder.setDateClose(orderDate);
				metaDataOrder.setTimeClose(orderTime);
				
				MessageSaveTableOrder msgSave= new MessageSaveTableOrder(bones,metaDataOrder);
				try {
					mainMenu.ms.client.sendToServer(msgSave);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					
				
				} catch (IOException ei) {
					// TODO Auto-generated catch block
					ei.printStackTrace();
				}
				
				
				
				
				
				MessageCloseTable msgCloseTable=new MessageCloseTable(bones,metaDataOrder); 
				try {
					mainMenu.ms.client.sendToServer(msgCloseTable);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					
				
				} catch (IOException ei) {
					// TODO Auto-generated catch block
					ei.printStackTrace();
				}
				
			}
		});
		
		
		menuButtonsPanel.add(butSave);
		menuButtonsPanel.add(butCancel);
		menuButtonsPanel.add(butReturn);
		menuButtonsPanel.add(butHeshbon);
		
		return menuButtonsPanel;
	}

	public Vector<Bon> getBones() {
		return bones;
	}

	public void setBones(Vector<Bon> bones) {
		this.bones = bones;
		this.mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
		this.mainMenu.getContentPane().add(new PfPtihatShulhanLeftRigtPanels(mainMenu,menues,bones,metaDataOrder), BorderLayout.CENTER);					
		this.mainMenu.getContentPane().validate();
		
	}
	
	
	
	
	
	
	
}
